package com.easy.dfcapi.service.impl;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.common.utils.SpringContextHolder;
import com.easy.common.utils.StringUtil;
import com.easy.dfcapi.entity.*;
import com.easy.dfcapi.mapper.*;
import com.easy.dfcapi.res.*;
import com.easy.dfcapi.service.ApiProductService;
import com.easy.dfcapi.utils.ApiSessionUser;
import com.easy.dfcapi.utils.ServletHelper;
import com.easy.modules.sys.entity.Para;
import com.easy.modules.sys.mapper.DictMapper;
import com.easy.modules.sys.mapper.ParaMapper;
import io.rong.RongCloud;
import io.rong.models.TokenResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by VULCANTSeries on 2017/5/10.
 */
@Service
@Transactional(readOnly = true)
public class ApiProductServiceImpl implements ApiProductService {

    @Autowired
    private ApiUserMapper userMapper;

    @Autowired
    private ApiStoreMapper storeMapper;

    @Autowired
    private ApiBrandMapper brandMapper;

    @Autowired
    private ApiCategoryMapper categoryMapper;

    @Autowired
    private ApiProductMapper productMapper;

    @Autowired
    private ParaMapper paraMapper;

    @Autowired
    private DictMapper dictMapper;

    @Autowired
    private ApiSkuColorMapper skuColorMapper;

    @Autowired
    private ApiSkuSizeMapper skuSizeMapper;

    @Autowired
    private ApiAttributesMapper attributesMapper;

    @Autowired
    private ApiStockMapper stockMapper;

    @Autowired
    private ApiProductImgMapper productImgMapper;

    @Autowired
    private ApiProductVideoMapper productVideoMapper;

    @Autowired
    private ApiProductAttrMapper productAttrMapper;

    @Autowired
    private ApiProductAuthMapper productAuthMapper;

    @Override
    @Transactional(readOnly = false)
    public Result BrandAdd(Response<BrandRes> response) throws Exception {
        Result rt = new Result();
        try {
            BrandRes data = response.getData(BrandRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }

            Integer isAudit = 0;
            Para para  = paraMapper.getByParaName("brandAudit");
            if(para != null){
                isAudit = StringUtil.toInt(para.getParaValue());
            }
            Date now = new Date();
            ApiBrand brand = new ApiBrand();
            brand.setBrandName(data.getBrandName());
            brand.setBrandImg(data.getBrandImg());
            brand.setBrandLogo(data.getBrandLogo());
            brand.setBrandDesc(data.getBrandDesc());

            brand.setStoreId(store.getStoreId());
            brand.setAuditStatus(isAudit);
            brand.preInsert(now);
            if(brandMapper.insert(brand)>0){
                rt.setStatus(EnumConfig.S_CODE.OK);
            }
            else {
                rt.setStatus(EnumConfig.S_CODE.FAIL);
            }


        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw  ex;
        }
        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result BrandMod(Response<BrandRes> response) throws Exception {
        Result rt = new Result();
        try {
            BrandRes data = response.getData(BrandRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }
            ApiBrand brand = brandMapper.get(data.getBrandId());
            if(brand == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_RECORD);
                return rt;
            }

            Integer isAudit = 0;
            Para para  = paraMapper.getByParaName("brandAudit");
            if(para != null){
                isAudit = StringUtil.toInt(para.getParaValue());
            }
            Date now = new Date();
            brand.setBrandName(data.getBrandName());
            brand.setBrandImg(data.getBrandImg());
            brand.setBrandLogo(data.getBrandLogo());
            brand.setBrandDesc(data.getBrandDesc());

            brand.preUpdate(now);
            brandMapper.update(brand);
            rt.setStatus(EnumConfig.S_CODE.OK);


        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw  ex;
        }
        return rt;
    }

    @Override
    public Result BrandList(Response<BrandRes> response) throws Exception {
        Result rt = new Result();
        try {
            BrandRes data = response.getData(BrandRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }
            List<Map<String, Object>> brandList = brandMapper.getMapByStoreId(store.getStoreId(),data.getAuditStatus());
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(brandList);

        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw  ex;
        }
        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result BrandDel(Response<BrandRes> response) throws Exception {
        Result rt = new Result();
        try {
            BrandRes data = response.getData(BrandRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }
            ApiBrand brand = brandMapper.get(data.getBrandId());
            if(brand == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_RECORD);
                return rt;
            }
            brandMapper.delete(brand);
            rt.setStatus(EnumConfig.S_CODE.OK);


        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw  ex;
        }
        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result CategoryAdd(Response<CategoryRes> response) throws Exception {
        Result rt = new Result();
        try {
            CategoryRes data = response.getData(CategoryRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            ApiCategory parent = new ApiCategory(0);
            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }

            Date now = new Date();
            ApiCategory category = new ApiCategory();
            category.setCateName(data.getCateName());
            category.setCateImg(data.getCateImg());

            if(StringUtil.isNotEmpty(data.getParentId())){
                parent = categoryMapper.get(data.getParentId());
                if(parent!=null){
                    category.setParent(parent);
                }
            }
            category.setIsShow(1);
            category.setSortNo(data.getSortNo());
            category.preInsert(now);
            category.setSonSum(0);

            if(categoryMapper.insert(category)>0){
                rt.setStatus(EnumConfig.S_CODE.OK);
            }
            else {
                rt.setStatus(EnumConfig.S_CODE.FAIL);
            }


        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }
        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result CategoryMod(Response<CategoryRes> response) throws Exception {
        Result rt = new Result();
        try {
            CategoryRes data = response.getData(CategoryRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }
            ApiCategory category = categoryMapper.get(data.getCateId());
            if(category == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_RECORD);
                return rt;
            }

            Date now = new Date();
            category.setCateName(data.getCateName());
            if(data.getCateImg()<1);
            category.setCateImg(data.getCateImg());

            if(StringUtil.isEmpty(data.getParentId())){
                ApiCategory parent = categoryMapper.get(data.getParentId());
                if(parent!=null){
                    category.setParent(parent);
                }
            }
            category.setIsShow(1);
            category.preUpdate(now);
            categoryMapper.update(category);
            rt.setStatus(EnumConfig.S_CODE.OK);


        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }
        return rt;
    }

    @Override
    public Result CategoryList(Response<CategoryRes> response) throws Exception {
        Result rt = new Result();
        try {
            CategoryRes data = response.getData(CategoryRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }
            if(StringUtil.isEmpty(data.getCateId())) {
                List<Map<String, Object>> categoryList = categoryMapper.getMapByMain();
                rt.setStatus(EnumConfig.S_CODE.OK);
                rt.setData(categoryList);
            }
            else {
                List<Map<String, Object>> categoryList = categoryMapper.getMapByCateId(data.getCateId());
                rt.setStatus(EnumConfig.S_CODE.OK);
                rt.setData(categoryList);
            }
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw  ex;
        }
        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result CategoryDel(Response<CategoryRes> response) throws Exception {
        Result rt = new Result();
        try {
            CategoryRes data = response.getData(CategoryRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }
            ApiCategory category = categoryMapper.get(data.getCateId());
            if(category == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_RECORD);
                return rt;
            }
            categoryMapper.delete(category);
            rt.setStatus(EnumConfig.S_CODE.OK);


        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw  ex;
        }
        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result ProductPreAdd(Response<ProductRes> response) throws Exception {
        Result rt = new Result();
        try {
            ProductRes data = response.getData(ProductRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }
            if(sessionUser.getStoreType()==1) {
            }
            else {

                ApiCategory category = categoryMapper.get(data.getCateId());
                if(category==null){
                    rt.setStatus(EnumConfig.S_CODE.PARAM_ERROR);
                    return rt;
                }

                List<Map<String,Object>> list = dictMapper.getMapByTypeName("dfcProductFactory");
                for(int i=0; i<list.size(); i++){
                    Map<String,Object> map = list.get(i);
                    String value = (String)map.get("labelValue");
                    if(value.equals("dfcSkuSize")){
                        List<Map<String,Object>> list1 = skuSizeMapper.getMapByStoreCateId(store.getStoreId(), category.getCateId());
                        if(list1.size()<1){
                            list1 = skuSizeMapper.getMapByStoreId(store.getStoreId());
                            if(list1.size()<1){
                                list1 = skuSizeMapper.getMap();
                            }
                        }
                        map.put("list",list1);
                    }
                    else if(value.equals("dfcSkuColor")){
                        List<Map<String,Object>> list1 = skuColorMapper.getMapByStoreCateId(store.getStoreId(), category.getCateId());
                        if(list1.size()<1){
                            list1 = skuColorMapper.getMapByStoreId(store.getStoreId());
                            if(list1.size()<1){
                                list1 = skuColorMapper.getMap();
                            }
                        }
                        map.put("list",list1);
                    }
                    else if(value.equals("dfcAttributes")){
                        List<Map<String,Object>> list1 = attributesMapper.getMapAllByCateId(category.getCateId());
                        if(list1.size()<1){
                            list1 = attributesMapper.getMapAllByStoreType(store.getStoreType());
                        }
                        map.put("list",list1);
                    }
                    else {
                        List<Map<String,Object>> list1 = dictMapper.getMapByTypeName(value);
                        map.put("list",list1);
                    }
                    list.set(i,map);
                }
                rt.setData(list);
                rt.setStatus(EnumConfig.S_CODE.OK);
            }


        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw  ex;
        }
        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result ProductAdd(Response<ProductRes> response) throws Exception {
        Result rt = new Result();
        try {
            Map<String,Object> resMap = new HashMap();
            resMap.put("stock",StockRes.class);
            resMap.put("images",ImgRes.class);
            resMap.put("videos",VideoRes.class);
            resMap.put("attrs",AttributesRes.class);
            resMap.put("auths",AuthRes.class);
            ProductRes data = response.getData(ProductRes.class, resMap);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }

            ApiBrand brand = brandMapper.get(data.getBrandId());
            if(brand==null){
                rt.setStatus(EnumConfig.S_CODE.PARAM_ERROR);
                return rt;
            }
            ApiCategory category = categoryMapper.get(data.getCateId());
            if(category==null){
                rt.setStatus(EnumConfig.S_CODE.PARAM_ERROR);
                return rt;
            }


            Date now = new Date();
            ApiProduct product = new ApiProduct();
            product.setProductNo(data.getProductNo());
            product.setProductName(data.getProductName());
            product.setProductNameStyle(data.getProductNameStyle());
            product.setBrandId(brand.getBrandId());
            product.setCateId(category.getCateId());
            product.setUnit(data.getUnit());
            product.setSupportRetail(data.getSupportRetail());
            product.setPrice(data.getPrice());
            product.setKeywords(data.getKeywords());
            product.setProductAddress(data.getProductAddress());
            product.setProductBrief(data.getProductBrief());
            product.setProductDesc(data.getProductDesc());
            product.setProductThumb(data.getProductThumb());
            product.setProductImg(data.getProductImg());
            product.setWarnNumber(data.getWarnNumber());
            product.setIsAloneSale(data.getIsAloneSale());
            product.setIsVirtual(data.getIsVirtual());
            product.setOrginPlace(data.getOrginPlace());
            product.setUnitSize(data.getUnitSize());
            product.setSizeDesc(data.getSizeDesc());
            product.setUnitWeight(data.getUnitWeight());
            product.setWeightDesc(data.getWeightDesc());
            product.setSupportArrears(data.getSupportArrears());
            product.setDeliveryTime(data.getDeliveryTime());
            product.setReturnTime(data.getReturnTime());
            product.setChangeTime(data.getChangeTime());
            product.setSaleTime(data.getSaleTime());
            product.setOffTime(data.getOffTime());
            product.setIntegral(data.getIntegral());
            product.setSellerNote(data.getSellerNote());
            product.setSortNo(data.getSortNo());
            product.setAgeGroup(data.getAgeGroup());
            product.setIsOnSale(data.getIsOnSale());
            product.setStoreId(store.getStoreId());
            product.preInsert(now);
            if(productMapper.insert(product)>0){
                List<StockRes> stockList = data.getStock();
                List<ImgRes> imageList = data.getImages();
                List<VideoRes> videoList = data.getVideos();
                List<AttributesRes> attrList = data.getAttrs();
                List<AuthRes> authList = data.getAuths();

                //写入库存开始
                if(stockList!=null  && stockList.size()>0) {
                    for (int i = 0; i < stockList.size(); i++) {
                        StockRes stockRes = stockList.get(i);
                        if (stockRes.getColorId() == null || stockRes.getColorId() < 1) { //将自定义颜色加到模版库
                            ApiSkuColor skuColor = new ApiSkuColor();
                            skuColor.setCateId(product.getCateId());
                            skuColor.setStoreId(product.getStoreId());
                            skuColor.setProductId(product.getProductId());
                            skuColor.setColorName(stockRes.getColorName());
                            skuColorMapper.insert(skuColor);
                        }
                        if (stockRes.getSizeId() == null || stockRes.getSizeId() < 1) { //将自定义尺寸加到模版库
                            ApiSkuSize skuSize = new ApiSkuSize();
                            skuSize.setCateId(product.getCateId());
                            skuSize.setStoreId(product.getStoreId());
                            skuSize.setProductId(product.getProductId());
                            skuSize.setSizeName(stockRes.getSizeName());
                            skuSizeMapper.insert(skuSize);
                        }

                        ApiStock stock = new ApiStock();
                        stock.setProductId(product.getProductId());
                        stock.setColorId(stockRes.getColorId());
                        stock.setSizeId(stockRes.getSizeId());
                        stock.setNum(stockRes.getNum());
                        stock.setPrice(stockRes.getPrice());
                        stockMapper.insert(stock);//写入库存
                    }
                }
                //写入库存结束


                //写入商品授权开始
                if(authList!=null  && authList.size()>0) {
                    for (int i = 0; i < authList.size(); i++) {
                        AuthRes authRes = authList.get(i);

                        ApiProductAuth productAuth = new ApiProductAuth();
                        productAuth.setProductId(product.getProductId());
                        productAuth.setUserId(authRes.getUserId());
                        productAuth.setAuthAllow(authRes.getAuthAllow());
                        productAuth.setAuthType(authRes.getAuthType());
                        productAuth.setCreateTime(now);
                        productAuthMapper.insert(productAuth);//写入商品图片
                    }
                }


                //写入商品图片开始
                if(imageList!=null  && imageList.size()>0) {
                    for (int i = 0; i < imageList.size(); i++) {
                        ImgRes imgRes = imageList.get(i);

                        ApiProductImg productImg = new ApiProductImg();
                        productImg.setProductId(product.getProductId());
                        productImg.setImgId(imgRes.getImgId());
                        productImg.setImgSno(i + 1);
                        productImg.setImgDesc(imgRes.getImgDesc());
                        productImgMapper.insert(productImg);//写入商品图片
                    }
                }
                //写入商品图片结束

                //写入商品视频开始
                if(videoList!=null  && videoList.size()>0) {
                    for (int i = 0; i < videoList.size(); i++) {
                        VideoRes videoRes = videoList.get(i);

                        ApiProductVideo productVideo = new ApiProductVideo();
                        productVideo.setProductId(product.getProductId());
                        productVideo.setVideoSno(i + 1);
                        productVideo.setVideoId(videoRes.getVideoId());
                        productVideo.setVideoCover(videoRes.getVideoCover());
                        productVideo.setVideoUrl(videoRes.getVideoUrl());
                        productVideo.setVideoDesc(videoRes.getVideoDesc());
                        productVideoMapper.insert(productVideo);//写入视频图片
                    }
                }
                //写入商品视频结束


                //写入商品属性开始
                if(attrList!=null  && attrList.size()>0) {
                    for (int i = 0; i < attrList.size(); i++) {
                        AttributesRes attr = attrList.get(i);

                        ApiProductAttr productAttr = new ApiProductAttr();
                        productAttr.setProductId(product.getProductId());
                        productAttr.setAttrId(attr.getAttrId());
                        productAttr.setAttrSelected(0);
                        productAttrMapper.insert(productAttr);
                    }
                }
                //写入商品属性结束

                rt.setStatus(EnumConfig.S_CODE.OK);
            }
            else {
                rt.setStatus(EnumConfig.S_CODE.FAIL);
            }


        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw  ex;
        }
        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result ProductMod(Response<ProductRes> response) throws Exception {
        Result rt = new Result();
        try {
            Map<String,Object> resMap = new HashMap();
            resMap.put("stock",StockRes.class);
            resMap.put("images",ImgRes.class);
            resMap.put("videos",VideoRes.class);
            resMap.put("attrs",AttributesRes.class);
            resMap.put("auths",AuthRes.class);
            ProductRes data = response.getData(ProductRes.class, resMap);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }

            ApiBrand brand = brandMapper.get(data.getBrandId());
            if(brand==null){
                rt.setStatus(EnumConfig.S_CODE.PARAM_ERROR);
                return rt;
            }
            ApiCategory category = categoryMapper.get(data.getCateId());
            if(category==null){
                rt.setStatus(EnumConfig.S_CODE.PARAM_ERROR);
                return rt;
            }


            Date now = new Date();
            ApiProduct product = productMapper.get(data.getProductId());
            if(product==null){
                rt.setStatus(EnumConfig.S_CODE.PARAM_ERROR);
                return rt;
            }
            product.setProductNo(data.getProductNo());
            product.setProductName(data.getProductName());
            product.setProductNameStyle(data.getProductNameStyle());
            product.setBrandId(brand.getBrandId());
            product.setCateId(category.getCateId());
            product.setUnit(data.getUnit());
            product.setSupportRetail(data.getSupportRetail());
            product.setPrice(data.getPrice());
            product.setKeywords(data.getKeywords());
            product.setProductAddress(data.getProductAddress());
            product.setProductBrief(data.getProductBrief());
            product.setProductDesc(data.getProductDesc());
            product.setProductThumb(data.getProductThumb());
            product.setProductImg(data.getProductImg());
            product.setWarnNumber(data.getWarnNumber());
            product.setIsAloneSale(data.getIsAloneSale());
            product.setIsVirtual(data.getIsVirtual());
            product.setOrginPlace(data.getOrginPlace());
            product.setUnitSize(data.getUnitSize());
            product.setSizeDesc(data.getSizeDesc());
            product.setUnitWeight(data.getUnitWeight());
            product.setWeightDesc(data.getWeightDesc());
            product.setSupportArrears(data.getSupportArrears());
            product.setDeliveryTime(data.getDeliveryTime());
            product.setReturnTime(data.getReturnTime());
            product.setChangeTime(data.getChangeTime());
            product.setSaleTime(data.getSaleTime());
            product.setOffTime(data.getOffTime());
            product.setIntegral(data.getIntegral());
            product.setSellerNote(data.getSellerNote());
            product.setSortNo(data.getSortNo());
            product.setAgeGroup(data.getAgeGroup());
            product.setIsOnSale(data.getIsOnSale());
            product.setStoreId(store.getStoreId());
            product.preUpdate(now);
            if(productMapper.update(product)>0){
                List<StockRes> stockList = data.getStock();
                List<ImgRes> imageList = data.getImages();
                List<VideoRes> videoList = data.getVideos();
                List<AttributesRes> attrList = data.getAttrs();
                List<AuthRes> authList = data.getAuths();

                //写入库存开始
                stockMapper.deleteByProductId(product.getProductId());
                if(stockList!=null  && stockList.size()>0) {
                    for (int i = 0; i < stockList.size(); i++) {
                        StockRes stockRes = stockList.get(i);
                        if (stockRes.getColorId() == null || stockRes.getColorId() < 1) { //将自定义颜色加到模版库
                            ApiSkuColor skuColor = new ApiSkuColor();
                            skuColor.setCateId(product.getCateId());
                            skuColor.setStoreId(product.getStoreId());
                            skuColor.setProductId(product.getProductId());
                            skuColor.setColorName(stockRes.getColorName());
                            skuColorMapper.insert(skuColor);
                        }
                        if (stockRes.getSizeId() == null || stockRes.getSizeId() < 1) { //将自定义尺寸加到模版库
                            ApiSkuSize skuSize = new ApiSkuSize();
                            skuSize.setCateId(product.getCateId());
                            skuSize.setStoreId(product.getStoreId());
                            skuSize.setProductId(product.getProductId());
                            skuSize.setSizeName(stockRes.getSizeName());
                            skuSizeMapper.insert(skuSize);
                        }

                        ApiStock stock = new ApiStock();
                        stock.setProductId(product.getProductId());
                        stock.setColorId(stockRes.getColorId());
                        stock.setSizeId(stockRes.getSizeId());
                        stock.setNum(stockRes.getNum());
                        stock.setPrice(stockRes.getPrice());
                        stockMapper.insert(stock);//写入库存
                    }
                }
                //写入库存结束


                //写入商品授权开始
                productAuthMapper.deleteByProductId(product.getProductId());
                if(authList!=null  && authList.size()>0) {
                    for (int i = 0; i < authList.size(); i++) {
                        AuthRes authRes = authList.get(i);

                        ApiProductAuth productAuth = new ApiProductAuth();
                        productAuth.setProductId(product.getProductId());
                        productAuth.setUserId(authRes.getUserId());
                        productAuth.setAuthAllow(authRes.getAuthAllow());
                        productAuth.setAuthType(authRes.getAuthType());
                        productAuth.setCreateTime(now);
                        productAuthMapper.insert(productAuth);//写入商品图片
                    }
                }


                //写入商品图片开始
                productImgMapper.deleteByProductId(product.getProductId());
                if(imageList!=null  && imageList.size()>0) {
                    for (int i = 0; i < imageList.size(); i++) {
                        ImgRes imgRes = imageList.get(i);

                        ApiProductImg productImg = new ApiProductImg();
                        productImg.setProductId(product.getProductId());
                        productImg.setImgId(imgRes.getImgId());
                        productImg.setImgSno(i + 1);
                        productImg.setImgDesc(imgRes.getImgDesc());
                        productImgMapper.insert(productImg);//写入商品图片
                    }
                }
                //写入商品图片结束

                //写入商品视频开始
                productVideoMapper.deleteByProductId(product.getProductId());
                if(videoList!=null  && videoList.size()>0) {
                    for (int i = 0; i < videoList.size(); i++) {
                        VideoRes videoRes = videoList.get(i);

                        ApiProductVideo productVideo = new ApiProductVideo();
                        productVideo.setProductId(product.getProductId());
                        productVideo.setVideoSno(i + 1);
                        productVideo.setVideoId(videoRes.getVideoId());
                        productVideo.setVideoCover(videoRes.getVideoCover());
                        productVideo.setVideoUrl(videoRes.getVideoUrl());
                        productVideo.setVideoDesc(videoRes.getVideoDesc());
                        productVideoMapper.insert(productVideo);//写入视频图片
                    }
                }
                //写入商品视频结束


                //写入商品属性开始
                productAttrMapper.deleteByProductId(product.getProductId());
                if(attrList!=null  && attrList.size()>0) {
                    for (int i = 0; i < attrList.size(); i++) {
                        AttributesRes attr = attrList.get(i);

                        ApiProductAttr productAttr = new ApiProductAttr();
                        productAttr.setProductId(product.getProductId());
                        productAttr.setAttrId(attr.getAttrId());
                        productAttr.setAttrSelected(0);
                        productAttrMapper.insert(productAttr);
                    }
                }
                //写入商品属性结束

                rt.setStatus(EnumConfig.S_CODE.OK);
            }
            else {
                rt.setStatus(EnumConfig.S_CODE.FAIL);
            }


        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw  ex;
        }
        return rt;
    }

    @Override
    public Result ProductList(Response<ProductRes> response) throws Exception {
        Result rt = new Result();
        try {
            ProductRes data = response.getData(ProductRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }
            List<Map<String,Object>> productList = productMapper.getMapByStoreId(store.getStoreId(), data.getIsOnSale(), data.getLimitIndex(), data.getLimitSize());
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(productList);

        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw  ex;
        }
        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result ProductDel(Response<ProductRes> response) throws Exception {
        Result rt = new Result();
        try {
            ProductRes data = response.getData(ProductRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }
            ApiProduct product = productMapper.get(data.getProductId());
            if(product == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_RECORD);
                return rt;
            }

            //删除库存
            stockMapper.deleteByProductId(product.getProductId());

            //删除商品授权
            productAuthMapper.deleteByProductId(product.getProductId());

            //删除商品图片
            productImgMapper.deleteByProductId(product.getProductId());

            //删除商品视频
            productVideoMapper.deleteByProductId(product.getProductId());

            //删除商品属性
            productAttrMapper.deleteByProductId(product.getProductId());

            //删除商品
            productMapper.delete(product);

            rt.setStatus(EnumConfig.S_CODE.OK);


        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw  ex;
        }
        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result ProductSales(Response<ProductRes> response) throws Exception {
        Result rt = new Result();
        try {
            ProductRes data = response.getData(ProductRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }
            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            ApiStore store = storeMapper.getByStoreId(sessionUser.getStoreId());
            if(store==null){
                rt.setStatus(EnumConfig.S_CODE.STORE_NOT_EXIST);
                return rt;
            }
            ApiProduct product = productMapper.get(data.getProductId());
            if(product == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_RECORD);
                return rt;
            }
            if(product.getIsOnSale()==2){
                rt.setStatus(EnumConfig.S_CODE.PRODUCT_IS_FORCE_DOWN);
                return rt;
            }
            if(data.getIsOnSale()==1 && product.getIsOnSale()==0){
                productMapper.setOnSale(product.getProductId());
            }
            if(data.getIsOnSale()==0 && product.getIsOnSale()==1){
                productMapper.setOffSale(product.getProductId());
            }
            rt.setStatus(EnumConfig.S_CODE.OK);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw  ex;
        }
        return rt;
    }
}
